<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 运费模板设置
 *
 * @author      Eden
 * @datetime    2016/12/16 17:53
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Express extends ADM_Controller
{
    // 参数错误
    private $error = '';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主页面
     */
    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/goods/express.html');
    }

    public function getSetting()
    {
        $this->load->model('admin/goods/Express_Model', 'e');
        $baseId = intval($this->baseId);
        if ($this->role != 'base') {
            $baseId = 0;
        }
        $res = $this->e->getSetting($baseId);
        if (empty($res)) {
            $this->output->send(array('state' => false));
        }

        $res['firstCost'] = money_format('%i', $res['firstCost'] / 100);
        $res['incrCost'] = money_format('%i', $res['incrCost'] / 100);

        $this->output->send(array('state' => true, 'data' => $res));
    }

    public function setSetting()
    {
        $send['state'] = false;
        do {

            $params = $this->input->post();
            if (!$this->checkParams($params)) {
                $send['error'] = $this->error;
                break;
            }

            $baseId = intval($this->baseId);
            if ($this->role != 'base') {
                $baseId = 0;
            }

            $this->load->model('admin/goods/Express_Model', 'e');
            $res = $this->e->setSetting($params, $baseId);
            if ($res) {
                $send['state'] = true;
            } else {
                $send['error'] = '设置失败';
            }

        } while(false);
        $this->output->send($send);
    }

    private function checkParams($params)
    {
        if (empty($params['tmpName']) || empty($params['dispatchAddr']) || empty($params['payer'])) {
            $this->error = '参数错误';
            return false;
        }

        if ($params['payer'] == 'buyer'){
            $w = $params['count'];
            if (empty($w['countType']) || empty($w['lists'])) {
                $this->error = '参数错误';
                return false;
            }
        }

        return true;
    }
}
